package com.supostacks.infrastructure.repository;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supostacks.domain.repository.ICategoryRepository;
import com.supostacks.domain.vo.CategoryVo;
import com.supostacks.domain.vo.ResourceInfoVo;
import com.supostacks.infrastructure.context.DefaultContext;
import com.supostacks.infrastructure.context.EntityUtil;
import com.supostacks.infrastructure.dao.CategoryDao;
import com.supostacks.infrastructure.po.Category;
import com.supostacks.infrastructure.po.ResourceInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class CategoryRepository extends DefaultContext implements ICategoryRepository {

    @Resource
    private CategoryDao categoryDao;

    @Override
    public String insert(CategoryVo categoryVo) {
        try {
            Category category = new Category();
            EntityUtil.copyFields(categoryVo, category);

            int insert = categoryDao.insert(category);
            if (insert > 0) {
                return category.getId();
            }
            return null;

        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(String id) {
        categoryDao.deleteById(id);
    }

    @Override
    public String updateById(CategoryVo categoryVo) {
        try {
            Category category = new Category();
            EntityUtil.copyFields(categoryVo, category);
            int i = categoryDao.updateById(category);
            if (i > 0) {
                return category.getId();
            }
            return "";
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public CategoryVo selectById(String id) {
        Category category = categoryDao.selectById(id);
        if (category != null) {
            return poToVo(category);
        }
        return null;
    }

    @Override
    public List<CategoryVo> selectList(JSONObject param) {
        QueryWrapper<Category> queryWrapper = where(param);
        List<Category> categoryInfos = categoryDao.selectList(queryWrapper);
        return buildList(categoryInfos);
    }

    @Override
    public JSONObject selectByPage(JSONObject options, int pageNo, int pageSize) {
        QueryWrapper<Category> queryWrapper = where(options);

        Page<Category> infos = new Page<>(pageNo, pageSize);
        IPage<Category> iPage = categoryDao.selectPage(infos, queryWrapper);

        List<Category> records = iPage.getRecords();

        JSONObject result = new JSONObject();
        result.put("total", records.size());
        result.put("list", records);

        return result;
    }

    public List<CategoryVo> buildList(List<Category> categoryInfos) {
        return categoryInfos.stream().map(this::poToVo).collect(Collectors.toList());
    }


    public CategoryVo poToVo(Category category) {
        try {
            CategoryVo categoryVo = new CategoryVo();
            EntityUtil.copyFields(category, categoryVo);
            return categoryVo;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
